package com.music.profile.dao.impl;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import com.music.profile.PMF;
import com.music.profile.beans.Song;
import com.music.profile.constants.SongState;

public class SongDAOImpl implements SongDAO {

	/* (non-Javadoc)
	 * @see com.music.profile.dao.impl.SongDAO#createSong(java.lang.String, java.lang.Long, java.lang.Long, com.music.profile.constants.SongState, java.lang.Long)
	 */
	public Long createSong(String name, Long composerId, Long preferredUrlId,
			SongState songState, Long createdMemberId) {
		Song song = new Song(null, name,composerId,preferredUrlId,
				songState, createdMemberId, new Date(), new Date());
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			song = persistenceManager.makePersistent(song);
		} finally {
			persistenceManager.close();
		}
		return song.getSongId();
	}

	/* (non-Javadoc)
	 * @see com.music.profile.dao.impl.SongDAO#updateSong(com.music.profile.beans.Song)
	 */
	public boolean updateSong(Song song) {
		// Set the last update date to current date.
		song.setLastUpdateDate(new Date());
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			song = persistenceManager.makePersistent(song);
		} finally {
			persistenceManager.close();
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see com.music.profile.dao.impl.SongDAO#getSong(java.lang.Long)
	 */
	public Song getSong(Long songId) {
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();

		return getSong(songId, persistenceManager);

	}
	/**
	 * This method is created to handle transactionally getting the song item.
	 * The transaction is maintained by the persistence manager. So, we are having it in one of the arguments.
	 * @param songId
	 * @param persistenceManager
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Song getSong(Long songId,
			PersistenceManager persistenceManager) {
		Query query = persistenceManager.newQuery(Song.class);
		query.setFilter("songId == songIdParam");
		query.declareParameters("String songParam");
		List<Song> results;
		results = (List<Song>) query.execute(songId);
		if (results == null || results.isEmpty()) {
			return null;
		}
		return results.get(0);
	}
	/* (non-Javadoc)
	 * @see com.music.profile.dao.impl.SongDAO#deleteSong(java.lang.Long)
	 */
	public boolean deleteSong(Long songId) {
		PersistenceManager persistenceManager = PMF.get().getPersistenceManager();
		Transaction tx = persistenceManager.currentTransaction();

		try {
			tx.begin(); // Begin the transaction
			Song song = getSong(songId, persistenceManager); // check its existence
			if (song == null) {
				// No song exists with the given id.
				return false;
			}
			persistenceManager.deletePersistent(song); // Delete if exists.
			tx.commit();
		} finally {
			if(tx.isActive()) {
				tx.rollback();
			}
			persistenceManager.close();
		}

		return true;
	}

	
}
